import { login, logout, getInfo } from '@/api/login'
import {
  getToken,
  setToken,
  removeToken,
  removeUserId,
  removeStoreCode,
  setStoreRoleId,
  removeStoreRoleId
} from '@/utils/auth'

const user = {
  state: {
    token: getToken(),
    name: '',
    avatar: '',
    roles: [],
    permissions: [],
    userId: '',
    storeCoding: '',
    storeRoleId: ''
  },

  mutations: {
    SET_TOKEN: (state, token) => {
      state.token = token
    },
    SET_NAME: (state, name) => {
      state.name = name
    },
    SET_AVATAR: (state, avatar) => {
      state.avatar = avatar
    },
    SET_ROLES: (state, roles) => {
      state.roles = roles
    },
    SET_PERMISSIONS: (state, permissions) => {
      state.permissions = permissions
    },
    SET_USER_ID: (state, userId) => {
      state.userId = userId
    },
    SET_STORE_CODE: (state, storeCoding) => {
      state.storeCoding = storeCoding
    },
    SET_STORE_ROLE_ID: (state, storeRoleId) => {
      state.storeRoleId = storeRoleId
    }
  },

  actions: {
    // 登录
    Login({ commit }, userInfo) {
      const storeUserPhone = userInfo.storeUserPhone
      const storeUserPwd = userInfo.storeUserPwd
      const logType = userInfo.logType
      const ip = userInfo.ip
      return new Promise((resolve, reject) => {
        login(storeUserPhone, storeUserPwd, logType, ip).then(res => {
          setToken(res.body.token)
          localStorage.setItem('token',res.body.token)
          commit('SET_TOKEN', res.body.token)
          resolve()
        }).catch(error => {
          reject(error)
        })
      })
    },

    // 获取用户信息
    GetInfo({ commit, state }) {
      return new Promise((resolve, reject) => {
        getInfo().then(res => {
          const avatar = require('@/assets/images/profile.jpg')
          if (res.body.storeRoleId && res.body.storeRoleId.length > 0) { // 验证返回的roles是否是一个非空数组
            commit('SET_ROLES', ['admin'])
            commit('SET_USER_ID', res.body.storeUserId)
            commit('SET_STORE_CODE', res.body.storeCoding)
            commit('SET_STORE_ROLE_ID', res.body.storeRoleId)
            commit('SET_PERMISSIONS', ["*:*:*"])
            setStoreRoleId(res.body.storeRoleId)
          } else {
            commit('SET_ROLES', ['ROLE_DEFAULT'])
          }
          commit('SET_NAME', res.body.storeUserName)
          commit('SET_AVATAR', avatar)
          resolve(res)
        }).catch(error => {
          reject(error)
        })
      })
    },

    // 退出系统
    LogOut({ commit, state }) {
      return new Promise((resolve, reject) => {
        logout().then(() => {
          commit('SET_TOKEN', '')
          commit('SET_ROLES', [])
          commit('SET_PERMISSIONS', [])
          commit('SET_USER_ID', '')
          commit('SET_STORE_CODE', '')
          commit('SET_STORE_ROLE_ID', '')
          removeToken()
          removeUserId()
          removeStoreCode()
          removeStoreRoleId()
          resolve()
        }).catch(error => {
          reject(error)
        })
      })
    },

    // 前端 登出
    FedLogOut({ commit }) {
      return new Promise(resolve => {
        commit('SET_TOKEN', '')
        removeToken()
        resolve()
      })
    }
  }
}

export default user
